/**
 * Copyright ©2016-2022 tianyu education Corporation, All Rights Reserved
 * http://www.whty.com.cn/
 */
package profile.hujia.shiro.study.service.config;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

/**
 * class info
 *
 * @author hujia
 * @date 2019-12-10 17:27
 */
@Configuration
public class JedisConfig {

    @Autowired
    ArgConfig argConfig;


    @Bean
    JedisCluster jedisCluster() {
        Set<HostAndPort> nodes = new HashSet<>();
        for (int i = 0; i < argConfig.getHosts().size(); ++i) {
            nodes.add(new HostAndPort(argConfig.getHosts().get(i), argConfig.getPorts().get(i)));
        }
        GenericObjectPoolConfig pool = new GenericObjectPoolConfig();
        pool.setMaxIdle(argConfig.getMaxIdle());
        pool.setMinIdle(argConfig.getMinIdle());
        pool.setMaxTotal(argConfig.getMaxTotal());
        pool.setMaxWaitMillis(argConfig.getMaxWaitMillis());
        return new JedisCluster(nodes, argConfig.getTimeout(), pool);
    }
}